﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BA2009.Entities;
using BA2009.Context;

namespace BA2009.Data.DataAccess
{
    public sealed class BannerPositionMapRepository: RepositoryBase<BannerPositionMap>,IBannerPositionMapRepository
     {

          public override IQueryable<BannerPositionMap> FindAll()
          {
              return from m in ReadOnlyContext.BannerPositionMaps
                                          select new BA2009.Entities.BannerPositionMap
                                          {
                                               BannerID= m.BannerID,
                                               BannerPositionID=m.BannerPositionID
                                          };
          }

          public override BannerPositionMap GetById( int id )
          {
               return (from m in ReadOnlyContext.BannerPositionMaps
                       select new BA2009.Entities.BannerPositionMap
                       {
                            BannerID=m.BannerID,
                            BannerPositionID=m.BannerPositionID
                       }).SingleOrDefault();
          }

          protected override void Save( object linqEnity, BA2009.DataAccess.Context.BA2009Context dbContext )
          {
               var bannerPositionMap= linqEnity as BA2009.DataAccess.Entities.BannerPositionMap;

               dbContext.BannerPositionMaps.InsertOnSubmit(bannerPositionMap);
               dbContext.SubmitChanges();
          }

          protected override void Delete( int id, BA2009.DataAccess.Context.BA2009Context dbContext )
          {
               var bannerPositionMap =(from m in dbContext.BannerPositionMaps
                                       where m.BannerPositionID==id
                                       select m).SingleOrDefault();

               dbContext.BannerPositionMaps.DeleteOnSubmit(bannerPositionMap);
               dbContext.SubmitChanges();
          }

          protected override void Update( object itemToUpdate, object orginialItem, BA2009.DataAccess.Context.BA2009Context dbContext )
          {
               var entity= itemToUpdate as BA2009.DataAccess.Entities.BannerPositionMap;
               var originial= orginialItem as BA2009.DataAccess.Entities.BannerPositionMap;

               dbContext.BannerPositionMaps.Attach(entity, originial);
               dbContext.SubmitChanges();
          }

          protected override object GetLinqEntity( int id )
          {
               return (from m in ReadOnlyContext.BannerPositionMaps
                            where(m.BannerPositionID== id)
                            select m).SingleOrDefault();
          }
     }
}
